﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 资源部关系
/// </summary>
public  class AssetBundleRelation
{
    private AssetBundleLoader bundleLoader;
    /// <summary>
    /// 资源加载进度
    /// </summary>
    private AssetBundleLoadProgress _onProgress;
    public AssetBundleLoadProgress onProgress { get { return _onProgress; } }
    /// <summary>
    /// 资源加载完成
    /// </summary>
    public AssetBundleComplete onComplete;
    /// <summary>
    /// AssetBundle资源名称
    /// </summary>
    private string assetBundleName;

    public string bundleNmae { get { return assetBundleName; } }

    private bool _finished;
    public bool finished { get { return _finished; } }

    public AssetBundleRelation(string assetBundleName, AssetBundleLoadProgress onProgress)
    {
        this.assetBundleName = assetBundleName;
        this._onProgress = onProgress;
        this._finished = false;
        bundleLoader = new AssetBundleLoader(this.OnLoadAssetBundleComplet, _onProgress, assetBundleName);
        dependenceBundle = new List<string>();
        referenceBundleList = new List<string>();
    }

    void OnLoadAssetBundleComplet(string assetBundleName)
    {
        this._finished = true;
        onComplete?.Invoke(assetBundleName);
    }
    #region 依赖关系
    private List<string> dependenceBundle = null;
    /// <summary>
    /// 添加依赖关系
    /// </summary>
    /// <param name="bundleName"></param>
    public void AddDependencies(string bundleName)
    {
        if (string.IsNullOrEmpty(bundleName) || dependenceBundle.Contains(bundleName)) return;
        dependenceBundle.Add(bundleName);
    }
    /// <summary>
    /// 移除依赖关系
    /// </summary>
    /// <param name="bundleName"></param>
    public void RemoveDependencies(string bundleName)
    {
        if (dependenceBundle.Contains(bundleName)) dependenceBundle.Remove(bundleName);
    }
   
    /// <summary>
    /// 获取全部依赖关系
    /// </summary>
    /// <returns></returns>
    public string[] GetAllDependencies()
    {
        return dependenceBundle.ToArray();
    }

    #endregion

    #region 被依赖关系
    /// <summary>
    /// 全部被依赖关系
    /// </summary>
    private List<string> referenceBundleList;

    public void AddReferencies(string bundleName)
    {
        if (string.IsNullOrEmpty(bundleName) || referenceBundleList.Contains(bundleName)) return;
        referenceBundleList.Add(bundleName);
    }
    /// <summary>
    /// 移除依赖关系
    /// </summary>
    /// <param name="bundleName"></param>
    public bool RemoveReferencies(string bundleName)
    {
        if (referenceBundleList.Contains(bundleName))
        {
            referenceBundleList.Remove(bundleName);
            if (referenceBundleList.Count > 0) return false;
            else
            {
                return true;
            }
        }
        return false;
    }
    /// <summary>
    /// 获取全部被依赖关系
    /// </summary>
    /// <returns></returns>
    public string[] GetAllReferencies()
    {
        return referenceBundleList.ToArray();
    }
    #endregion

    public IEnumerator Load()
    {
        yield return bundleLoader?.Load();
    }
    public AssetBundle Asset { get { return bundleLoader.Asset; } }

    public UnityEngine.Object LoadAsset(string assetName)
    {
        if (bundleLoader == null)
        {
            Debug.LogError("当前bundleLoader is null");
            return null;
        }
        return bundleLoader.LoadAsset(assetName);
    }
    public T LoadAsset<T>(string assetName) where T : UnityEngine.Object
    {
        if (bundleLoader == null)
        {
            Debug.LogError("当前bundleLoaderis null");
            return null;
        }
        return bundleLoader.LoadAsset<T>(assetName);
    }

    public T[] LoadAssetAll<T>() where T : UnityEngine.Object
    {
        return bundleLoader?.LoadAssetAll<T>();
    }
    /// <summary>
    /// 获取图集资源的全部子集
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="assetName"></param>
    /// <returns></returns>
    public UnityEngine.Object[] LoadAssetWithSubAssets(string assetName)
    {
        return bundleLoader?.LoadAssetWithSubAssets(assetName);
    }
    /// <summary>
    /// 卸载资源
    /// </summary>
    /// <param name="asset"></param>
    public void UnLoadAsset(UnityEngine.Object asset)
    {
        bundleLoader.UnLoadAsset(asset);
    }
    /// <summary>
    /// 释放assetbundle资源
    /// </summary>
    public void Dispose()
    {
        bundleLoader?.Dispose();
        bundleLoader = null;
    }
    /// <summary>
    /// 测试专用，获取当前assetbundle资源包的已经加载好的全部资源名称
    /// </summary>
    /// <returns></returns>
    public string[] GetAllAssetNames()
    {
        return bundleLoader?.GetAllAssetNames();
    }
    public void Trace()
    {
        bundleLoader?.Trace();
    }
}